package com.hq.shop09home.controller;

import com.hq.commons.utils.GsonUtils;
import com.hq.shop09home.constants.HomeConstants;
import com.hq.shop09home.feign.api.GoodsService;
import com.hq.shop09home.vo.GoodsType;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Controller
@Slf4j
public class IndexController {

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RedissonClient redissonClient;

    /**
     * @author: HQ
     * @return:
     * @time: 2021/7/13 18:56
     * @description: 一般存在Redis中的数据都是json数据，json可以跨平台跨语言
     * 缓存击穿问题
     * 缓存穿透问题---->用分布式锁解决
     * -
     * 真他妈的有钱啊
     */
    @GetMapping({"index.html", "/"})
    public String index(ModelMap modelMap) {
//        先查缓存
        String data = stringRedisTemplate.opsForValue().get(HomeConstants.HOME_GOODS_TYPE_LIST_KEY);
        if (StringUtils.isEmpty(data)) {
//            再去数据库中查
            data = queryGoodsTypeList();
        }
        List<GoodsType> goodsTypeList = (List<GoodsType>) GsonUtils.toObject(data, List.class);


//        放到ModelMap中
        modelMap.put("goodsList", goodsTypeList);

        return "index";
    }

    /**
     * @author: HQ
     * @return:
     * @time: 2021/7/13 17:59
     * @description: 从数据库中查询
     */
    private String queryGoodsTypeList() {

        List<GoodsType> newGtList = new ArrayList<>();
//        获取锁
        RLock lock = redissonClient.getLock("home-goods-type-lock");
//        加锁
        lock.lock();
        try {
            String data = stringRedisTemplate.opsForValue().get(HomeConstants.HOME_GOODS_TYPE_LIST_KEY);
            if (StringUtils.isEmpty(data)) {
                //                 处理业务
                //        1查询商品类别
                List<GoodsType> goodsList = goodsService.getGoodsList();

                for (GoodsType goodsType : goodsList) {
                    if (goodsType.getGoodsTypePid() == -1) {
                        newGtList.add(goodsType);
                    }
                }
//           寻找二级分类
                for (GoodsType goodsType : newGtList) {
                    for (GoodsType type : goodsList) {
                        if (type.getGoodsTypePid() == goodsType.getId()) {
                            goodsType.getChilderList().add(type);
                        }
                    }
                }
                //        把集合转成json字符串
                data = GsonUtils.toJSON(newGtList);
//        查询到数据要在缓存中放一份
                if (!StringUtils.isEmpty(data)) {
                    stringRedisTemplate.opsForValue().set(HomeConstants.HOME_GOODS_TYPE_LIST_KEY, data, 10, TimeUnit.DAYS);
                }else {
//                    解决缓存穿透的问题
                    stringRedisTemplate.opsForValue().set(HomeConstants.HOME_GOODS_TYPE_LIST_KEY, "null");
                }
            }
            return data;
        } finally {
//           释放锁
            lock.unlock();
        }
    }
}
